package com.harrison.web;

import com.harrison.annotation.SysLogger;
import com.harrison.dto.RespDTO;
import com.harrison.entity.User;
import com.harrison.service.UserService;
import com.harrison.util.BPwdEncodingUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: harrison
 * @Date: 2019/6/24 09:28
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "注册", notes = "username 和 password 为必须项")
    @PostMapping("/registry")
    @SysLogger("registry")
    public User createUser(@RequestBody User user){
        String entryPassword = BPwdEncodingUtils.BCryptPassword(user.getPassword());
        user.setPassword(entryPassword);
        return userService.createUser(user);
    }

    @ApiOperation(value = "登录", notes = "username和password为必选项")
    @PostMapping("/login")
    @SysLogger("login")
    public RespDTO login(
            @RequestParam String username,
            @RequestParam String password){
        //参数判读
        return userService.login(username,password);
    }

    @ApiOperation(value = "根据用户名获取用户", notes = "根据用户名获取用户")
    @PostMapping("/{username}")
    @PreAuthorize("hasRole('USER')")
    @SysLogger("getUserInfo")
    public RespDTO getUserInfo(@PathVariable("username") String username){
        //参数判读

        User user = userService.getUserInfo(username);
        return RespDTO.onSuc(user);
    }
}
